﻿using System;
using System.Text;
using System.Data;
using VIT.DataHelper.DataAccess;
using System.Data.SqlClient;

namespace VIT.Business
{
    public class BLFaqGroup
    {
        SQLSupport sqlSupport = new SQLSupport();

        #region Properties
        public int Id { get; set; }
        public int ParentId { get; set; }
        public string LanguageId { get; set; }
        public string Image { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        #endregion

        #region Constructor
        public BLFaqGroup()
        {
            this.Id = -1;
            this.ParentId = 0;
            this.LanguageId = string.Empty;
            this.Image = string.Empty;
            this.Title = string.Empty;
            this.Description = string.Empty;
        }
        #endregion

        #region Methods
        public DataTable GetAll()
        {
            string strQuery = "select Id, Title, ParentId, Image, Description from FaqGroup where LanguageID=@LanguageID and Published='True'";
            SqlParameter LanguageId = new SqlParameter("@LanguageID", this.LanguageId);
            return sqlSupport.ExecuteDataset(false, strQuery, LanguageId).Tables[0];

        }

        public DataTable GetList(int parentId)
        {
            string strQuery = "select Id, Title, ParentId, Image, Description from FaqGroup where LanguageID=@LanguageID and ParentId=@ParentId and Published='True'";
            SqlParameter LanguageId = new SqlParameter("@LanguageID", this.LanguageId);
            SqlParameter ParentId = new SqlParameter("@ParentId", parentId);
            return sqlSupport.ExecuteDataset(false, strQuery, LanguageId, ParentId).Tables[0];

        }

        public void GetSingle()
        {
            if (Exist())
            {
                string strQuery = "select ParentId, Title, Image, Description, Orders, Published from FaqGroup where LanguageID=@LanguageId and Id=@Id";
                SqlParameter Id = new SqlParameter("@Id", this.Id);
                SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
                DataTable dt = sqlSupport.ExecuteDataset(false, strQuery, Id, LanguageId).Tables[0];

                if (dt.Rows.Count > 0)
                {
                    this.Image = dt.Rows[0]["Image"].ToString();
                    this.Title = dt.Rows[0]["Title"].ToString();
                    this.Description = dt.Rows[0]["Description"].ToString();
                    if (!string.IsNullOrEmpty(dt.Rows[0]["ParentId"].ToString()))
                        this.ParentId = (int)dt.Rows[0]["ParentId"];
                }
            }
            else this.Id = -1;
        }

        public bool Exist()
        {
            string strQuery = "select Id from FaqGroup where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            return sqlSupport.ExecuteScalar(false, strQuery, Id) != null;
        }
        #endregion
    }
}
